// pages/duchuntao/unit5/case5-11-3/case5-11-3.js
var tempFilePaths, tempFilePath;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        msg: '',
        hidden: true
    },
    openFile: function () {
        var that = this;
        wx.chooseImage({
            success(res) {
                tempFilePath = res.tempFilePaths
                console.log("打开文件路径：" + tempFilePaths)
                that.setData({
                    imgPath: tempFilePaths[0],
                    hidden: false,
                    msg: "文件打开成功！"
                })
            }
        })
    },
    saveFile: function () {
        var that = this;
        wx.chooseImage({
            success(res) {
                console.log(res)
                let tempFilePaths = res.tempFilePaths
                wx.saveFile({
                    tempFilePath: tempFilePaths[0],
                    success(res) {
                        console.log("保存文件路径：" + res.saveFilePath);
                        that.setData({
                            hidden: false,
                            msg: "文件保存成功！"
                        })
                    }
                })
            }
        })
    },
    getSavedFileInfo: function () {
        var i, file;
        var that = this;
        wx.getSavedFileList({
            success: function (res) {
                if (res.fileList.length == 0) {
                    that.setData({
                        hidden: false,
                        msg: "没有文件信息"
                    })
                } else {
                    that.setData({
                        msg: ""
                    })
                    for (i = 0; i < res.fileList.length; i++) {
                        file = res.fileList[i];
                        let index = i + 1;
                        let zdymsg = "\n第" + index + "个文件的大小：" + file.size + "\n第" + index + "个文件的创建时间：" + file.createTime;
                        if (index == 1) {
                            zdymsg = "文件数量：" + res.fileList.length + zdymsg;
                        }
                        wx.getSavedFileInfo({
                            filePath: file.filePath,
                            success: function (res) {
                                that.setData({
                                    hidden: false,
                                    msg: that.data.msg + zdymsg
                                })
                            }
                        })
                    }
                }
            }
        })
    },
    removeSavedFile: function () {
        var i, file;
        var that = this;
        wx.getSavedFileList({
            success: function (res) {
                for (i = 0; i < res.fileList.length; i++) {
                    file = res.fileList[i];
                    wx.removeSavedFile({
                        filePath: file.filePath,
                    })
                    console.log("第" + (i + 1) + "个文件被删除！")
                }
                that.setData({
                    hidden: false,
                    msg: "文件被全部删除"
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})